﻿@model Tuple<List<deviceManage.Models.DeviceType>,int?>
@{
    ViewData["Title"] = "设备台账";
}
<style>
    .layui-form.search .layui-form-label{
        width:90px;
    }
</style>
 <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto search" lay-filter="app-content-list">
                    <table>
                        <tr>
                            <td>
                                <div class="layui-inline">
                                    <label class="layui-form-label">设备名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="layui-inline">
                                    <label class="layui-form-label" style="width:70px;">设备编号</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="deviceId" placeholder="请输入" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="layui-inline">
                                    <label class="layui-form-label" style="width: 60px;">设备类型</label>
                                    <div class="layui-input-inline">
                                        <select name="deviceTypeId">
                                            <option></option>
                                            @foreach (var type in Model.Item1)
                                            {
                                                <option value="@type.Id">@type.Name</option>
                                            }
                                        </select>
                                    </div>
                                </div>
                            </td>
                            <td rowspan="2" style="padding-left:10px;"> 
                                <div class="layui-inline">
                                    <button style="height:75px;padding: 0 10px;" class="layui-btn layuiadmin-btn-list" lay-submit="" lay-filter="search">
                                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                                    </button>
                                </div>
                            </td>
                            <td style="padding-left:10px;">
                                @if (1 != Model.Item2) {
                                    <button onclick="deviceIn()" class="layui-btn">设备入库</button>
                                    <button id="uploadBtn" class="layui-btn layuiadmin-btn-list" data-type="add">导入</button>
                                    <button onclick="exportDevice()" class="layui-btn layuiadmin-btn-list">导出</button>
                                }
                            </td>
                        </tr>
                        <tr>
                            <td colspan="3">
                                <div class="layui-inline">
                                    <label class="layui-form-label">固定资产编号</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="no" placeholder="请输入" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label" style="width:66px;">采购日期</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="start" class="layui-input date" id="test-laydate-start" placeholder="开始日期" lay-key="122">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label" style="width:66px;">结束日期</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="end" class="layui-input date" id="test-laydate-end" placeholder="结束日期" lay-key="123">
                                    </div>
                                </div>
                            </td>
                            <td style="padding-left:10px;">
                                @if (1 != Model.Item2)
                                {
                                    <button onclick="printTag()" class="layui-btn layui-btn-success" data-type="add">打印标签</button>
                                    <button style="width: 133px;" class="layui-btn layuiadmin-btn-list" onclick="downloadTemplate()">下载导入模板</button>
                                }

                            </td>
                        </tr>
                    </table> 
                </div>

                <div class="layui-card-body">                    
                    <table id="deviceTable" lay-filter="LAY-app-content-list"></table>
                </div>
            </div>
        </div>
 
<script type="text/javascript" src="~/js/deviceState.js"></script>
<script>
    
 

    var laydate = layui.laydate;
    var insStart = laydate.render({
        elem: '#test-laydate-start'
        , min: '1980-1-1'
        , done: function (value, date) {
            //更新结束日期的最小日期
            insEnd.config.min = lay.extend({}, date, {
                month: date.month - 1
            });

            //自动弹出结束日期的选择器
            insEnd.config.elem[0].focus();
        }
    });

    //结束日期
    var insEnd = laydate.render({
        elem: '#test-laydate-end'
        , min: '1980-1-1'
        , done: function (value, date) {
            //更新开始日期的最大日期
            insStart.config.max = lay.extend({}, date, {
                month: date.month - 1
            });
        }
    });

    form.render(null, 'app-content-list');

    //监听搜索
    form.on('submit(search)', function (data) {
        var field = data.field;
        //执行重载
        deviceTable.reload({
            where: field
        });
    });
     

    //加载table
    window.deviceTable = table.render({
            elem: '#deviceTable'
            , url: '/deviceInfo/list/' //模拟接口
            ,method: 'post'
            , height:"full-150"
            , page: true
            , cols: [[            
                { type: 'checkbox', fixed: 'left' }
                , { field: 'deviceName', title: '设备名称', minWidth: 180, templet: '<div><a class="link" onclick="showDetail(\'{{d.id}}\')"  href="#">{{d.name}}</a></div>'  }
                , { field: 'category', title: '设备类型分类', width: 120, templet: '<div>{{ getCategory(d.deviceType)}}</div>' }
                , { field: 'deviceType', title: '设备类型', width: 120, templet: '<div>{{ getType(d.deviceType)}}</div>' }
                , { field: 'model', title: '规格型号', width: 120, templet: '<div>{{ d.model}}</div>' }
                , { field: 'deviceId', title: '设备编号', width: 120, templet: '<div>{{ d.deviceId}}</div>' }
                    , { field: 'name', title: '固定资产编号', width: 120, templet: '<div>{{ d.no}}</div>' }
                    , { field: 'name', title: '所在机房', width: 120, templet: '<div>{{ d.room.name}}</div>' } 
                    , { field: 'name', title: '状态', width: 120, templet: '<div>{{ getDeviceState(d.status)}}</div>' } 
                    , { field: 'name', title: '采购日期', width: 120, templet: '<div>{{ getDate(d.buyDate)}}</div>' }
                    , { field: 'name', title: '接管日期', width: 120, templet: '<div>{{ getDate(d.receiveDate)}}</div>' }
                , { field: 'name', title: '出保日期', width: 120, templet: '<div>{{ getDate(d.serviceDate) }}</div>' }                    
                    , {
                        field: 'opr', title: '操作', width: 255, fixed:'right',
                    templet:
                    '<div class="layui-table-cell laytable-cell-11-0-8">'                
                        + ' {{ getButtons(d.id,d.status) }} '                    
                    + '</div>'
                    }
            ]]
            , skin: 'line'
        , done: function () {
            $("td").on("mouseenter", function () {                
                var cell = $(this).find(".layui-table-cell")[0];                
                if (cell.offsetWidth < cell.scrollWidth) {                    
                    var that = this;
                    var text = $(this).text();
                    layer.tips(text, that, {                        
                        time: 2000
                    });
                }
                   
                });
           }
    });
    var tableObj = window.deviceTable;

    function getDate(d) {
        if (d != null) {
            return d.substring(0, 10);
        } else {
            return "";
        }
    }
    var viewFlag = "@Model.Item2";
    //根据状态获取不同的按钮
    function getButtons(id, state) {    
        if (viewFlag == 1) return ""; //只读权限
        if (state == 1) {
            var html = ' <a class="layui-btn layui-btn-normal layui-btn-xs"   onclick="editDevice(\'' + id +'\')"  ><i class="layui-icon layui-icon-edit"></i>编辑</a>'
                + ' <a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteDevice(\'' + id +'\')"><i class="layui-icon layui-icon-delete"></i>删除</a> ' 
                +'<a class="layui-btn layui-btn-warm layui-btn-xs" onclick="goRepair(\'' + id + '\')"><i class="layui-icon layui-icon-util"></i>维修</a>'
                + '<a class="layui-btn  layui-btn-xs" onclick="goScrap(\'' + id + '\')"><i class="layui-icon layui-icon-close"></i>报废</a>';        
            return html;
        } else if (state == 2) {
            return '<a class="layui-btn layui-btn-warm layui-btn-xs" onclick="finishRepair(\'' + id + '\')"><i class="layui-icon layui-icon-util"></i>维修完成</a>';
        } else if (state == 5) {
            return '<a class="layui-btn layui-btn-warm layui-btn-xs" onclick="goRepair(\'' + id + '\')"><i class="layui-icon layui-icon-util"></i>维修</a>';
        } else if (state == 6) {
            return '<a class="layui-btn layui-btn-normal layui-btn-xs" onclick="revert(\'' + id + '\')"><i class="layui-icon layui-icon-return"></i>恢复</a>';
        } else {
            return "";
        } 
    }

    var winIndex = null;
    //设备入库
    function deviceIn() {
        winIndex = layer.open({
            type: 2,
            title:'设备入库',
            content: '/deviceInfo/deviceIn',
            area:["1100px","550px"]
        });
    }

    //设备编辑
    function editDevice(id) {
        winIndex = layer.open({
            type: 2,
            title: '编辑设备',
            content: "/deviceInfo/deviceIn?id="+id,
            area: ["1100px", "550px"]
        });
    }

    //维修页面
    function goRepair(id) {
        $.get("/deviceinfo/GoRepair?id="+id, function (str) {
            var index = layer.open({
                type: 1,
                title: '维修',
                content: str //注意，如果str是object，那么需要字符拼接。
            });

            form.render("", 'repairForm');
            laydate.render({ 'elem':'#beginDate'});

            //监听提交
            form.on('submit(saveRepair)', function (data) {
                var field = data.field; //获取提交的字段 
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.post("/deviceInfo/SaveRepair", field, function (d) { layer.msg(d.msg); layer.close(index);tableObj.reload();  });

            });
        });
    }
    //维修完成
    function finishRepair(id) {
        $.get("/deviceinfo/FinishRepair?id=" + id, function (str) {
            var index = layer.open({
                type: 1,
                title: '维修完成',
                content: str //注意，如果str是object，那么需要字符拼接。
            });

            form.render("", 'repairFinishForm');
            laydate.render({ 'elem': '#endDate' });

            //监听提交
            form.on('submit(saveFinishRepair)', function (data) {
                var field = data.field; //获取提交的字段 
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.post("/deviceInfo/SaveFinishRepair", field, function (d) { layer.msg(d.msg); layer.close(index); tableObj.reload(); });

            });
        });
    }

    //报废页面
    function goScrap(id) {
        $.get("/deviceinfo/Scrap?id=" + id, function (str) {
            var index = layer.open({
                type: 1,
                title: '设备报废',
                content: str
            });

            form.render("", 'scrapForm');
            laydate.render({ 'elem': '#scrapDate' });

            //监听提交
            form.on('submit(saveScrap)', function (data) {
                var field = data.field; //获取提交的字段 
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.post("/deviceInfo/SaveScrap", field, function (d) { layer.msg(d.msg); if (d.code == 1) { layer.close(index); tableObj.reload();} });

            });
        });
    }

    //删除设备
    function deleteDevice(id) {
        layer.confirm("确定要删除吗？", { title: '删除' }, function (index) {
            layer.close(index);
            $.post("/deviceInfo/deleteDevice?id=" + id, function (d) {
                layer.msg(d.msg);
                if (d.code == 1) {
                    tableObj.reload();
                }
            });
        });
    }

    //恢复设备
    function revert(id) {
        layer.confirm("确定要恢复设备吗？", { title: '恢复设备' }, function (index) {
            layer.close(index);
            $.post("/deviceInfo/revertDevice?id=" + id, function (d) {
                layer.msg(d.msg);
                if (d.code == 1) {
                    tableObj.reload();
                }
            });
        });
    }
    var printTagWin = null;
    function printTag() {
        var checkStatus = table.checkStatus('deviceTable'); //idTest 即为基础参数 id 对应的值
        if (checkStatus.data.length == 0) {
            layer.msg('请选择要打印的设备！');
            return;
        }        
        printTagWin = layer.open({
            type: 2,
            title:'打印',
            content: '/deviceinfo/printTag?id=' + checkStatus.data[0].id,
            area:["210px","280px"]
        }); 
    }

    function closePrintTagWin() {
        layer.close(printTagWin);
    }

    function exportDevice() {
        window.location.href ="/deviceInfo/Export"
    }

    upload.render({
        elem: "#uploadBtn" //绑定元素
        , url: '/deviceInfo/ImportExcel'//上传接口
        , accept: 'file'
        , done: function (res) {
            layer.msg(res.msg);
            if (res.code == 1) tableObj.reload();//上传成功，刷新附件table
        }
        , error: function () {
            layer.msg("上传失败！");
        }
    });

    //下载模板
    function downloadTemplate() {
        window.location.href = "/deviceInfo/DownloadTemplate";
    }

    function showDetail(id) {        
        layer.open({
            mask:0,
            type: 2,
            content: '/deviceInfo/Detail?id=' + id,
            area:["1000px","550px"]
        });
    }

    function getType(type) {
        if (type == null) return "";
        return type.name;
    }

    function getCategory(type) {
        if (type == null) return "";
        if (type.deviceTypeCategory == null) return "";
        return type.deviceTypeCategory.name;
    }
</script>

